//
//  TTServiceManager.swift
//  ServiceManager
//
//  Created by 张福润 on 2021/6/30.
//

import Foundation
import UIKit
import RxSwift
import TTHUD
import SwiftyUserDefaults

typealias VoidAction = () -> Void
typealias ChangeServiceAction = (VoidAction) -> Void
let COLOR_587FFF = UIColor.init(red: 88 / 255.0, green: 127 / 255.0, blue: 1, alpha: 1)
let COLOR_666666 = UIColor.init(red: 102 / 255.0, green: 102 / 255.0, blue: 102 / 255.0, alpha: 1)

enum PPServiceType: String {
    case dev
    case test
    case production
}

extension PPServiceType {
    var serviceName: String {
        switch self {
        case .dev: return "开发服"
        case .test: return "测试服"
        case .production: return "正式服"
        }
    }
}

extension DefaultsKeys {
    var pp_service_defaults_key: DefaultsKey<String> {
        .init("pp_service_defaults_key", defaultValue: PPServiceType.production.rawValue)
    }
}

class PPServiceManager: NSObject {
    fileprivate static var disposeBag: DisposeBag = DisposeBag()
    static let shared = PPServiceManager.init()

    
    fileprivate var currentServiceType: PPServiceType = PPServiceType(rawValue: Defaults[\.pp_service_defaults_key]) ?? .production
    
    var serviceType: PPServiceType {
        get {
            return self.currentServiceType
        }
        
        set {
            self.currentServiceType = newValue
            Defaults[\.pp_service_defaults_key] = newValue.rawValue
        }
    }
    
    // MARK: - Methods
}

extension PPServiceManager {
    func showServiceList(willChangeServiceAction: @escaping ChangeServiceAction) {
        ActionSheet(title: "切换服务器", message: "请选择需要切换的服务器")
            .addAction(title: "取消", style: .cancel, configure: { (_, action) in
                UIActionCustomTitleColor(action: action, color: COLOR_666666)
            })
            .addAction(title: PPServiceType.production.serviceName, configure: { (_, action) in
                UIActionCustomTitleColor(action: action, color: COLOR_587FFF)
            })
            .addAction(title: PPServiceType.test.serviceName, configure: { (_, action) in
                UIActionCustomTitleColor(action: action, color: COLOR_587FFF)
            })
            .addAction(title: PPServiceType.dev.serviceName, configure: { (_, action) in
                UIActionCustomTitleColor(action: action, color: COLOR_587FFF)
            })
            .rx.show()
            .subscribe(onNext: {(result) in
                if result.buttonIndex == 0 {
                    return
                }
                
                if result.buttonIndex == 1 {
                    // 正式服
                    self.changeService(service: PPServiceType.production, willChangeAction: willChangeServiceAction)
                    return
                }
                
                if result.buttonIndex == 2 {
                    self.changeService(service: PPServiceType.test, willChangeAction: willChangeServiceAction)
                    // 测试服
                    return
                }
                
                if result.buttonIndex == 2 {
                    self.changeService(service: PPServiceType.dev, willChangeAction: willChangeServiceAction)
                    // 开发服
                    return
                }
                
            })
            .disposed(by: PPServiceManager.disposeBag)
    }
    
    fileprivate func changeService(service: PPServiceType?, willChangeAction: ChangeServiceAction?) {
        guard let serviceType = service else {
            TTHUD.info("服务器切换错误, 请重试!")
            return
        }
        if serviceType == self.serviceType {
            TTHUD.info("当前已经是: \(serviceType.serviceName)")
            return
        }
        
        if let changeAction = willChangeAction {
            changeAction({
                self.serviceType = serviceType
            })
        } else {
            self.serviceType = serviceType
        }
    }
}

